<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/7
 * Time: 12:10
 */

namespace app\common\lib\exception;


use app\common\model\mysql\ErrorLog;
use Exception;
use think\exception\ClassNotFoundException;
use think\exception\HttpResponseException;
use think\exception\PDOException;
use think\exception\ErrorException;
use think\exception\DbException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\RouteNotFoundException;
use think\exception\TemplateNotFoundException;
use think\exception\ThrowableError;
use think\exception\ValidateException;
use think\facade\Log;

class Http extends Handle
{
    /**
     * @param Exception $e
     * @return \think\Response|void
     */
    public function render(Exception $e)
    {
        //日志记录
        $logInfo = '['.$e->getCode().']语法解析错误:'.$e->getMessage().'['.$e->getFile().':'.$e->getLine().']-['.date(
                'Y-m-d H:i:s'
            ).']';

        Log::record($logInfo, 'error');
        app(ErrorLog::class)->addData(
            [
                'log_info' => $logInfo,
                'log_url'  => request()->url(true),
                'log_time' => time(),
            ]
        );
        // 参数验证错误
        if ($e instanceof ValidateException && request()->isAjax()) {
            return json(
                [
                    'info'   => $e->getError(),
                    'status' => 0,
                ]
            );
        }

        // 捕捉系统抛出的异常并且是ajax请求
        if ($e instanceof ThrowableError && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        // 捕捉http请求异常并且是ajax请求
        if ($e instanceof HttpException) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        // 新增2020-02-09

        // 捕捉系统抛出的异常并且是ajax 请求
        if ($e instanceof \Exception && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        // 捕捉Db类抛出的异常
        if ($e instanceof DbException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        //捕捉PDO抛出异常并且是ajax请求
        if ($e instanceof PDOException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        //捕捉Error抛出异常并且是ajax请求
        if ($e instanceof ErrorException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        //捕捉HttpResponse抛出异常并且是ajax请求
        if ($e instanceof HttpResponseException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        //捕捉类不存在抛出异常并且是ajax请求
        if ($e instanceof ClassNotFoundException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        //捕捉路由不存在抛出异常并且是ajax请求
        if ($e instanceof RouteNotFoundException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        //捕捉模板不存在抛出异常并且是ajax请求
        if ($e instanceof TemplateNotFoundException && request()->isAjax()) {
            return json(
                [
                    'info'   => $logInfo,
                    'status' => 0,
                ]
            );
        }

        return parent::render($e);
    }
}